package com.hliushi.movie.web;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hliushi.movie.entity.MovieActor;
import com.hliushi.movie.service.MovieActorService;
import com.hliushi.movie.service.MovieDetailService;
import com.itheima.pinda.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 演员管理模块
 *
 * @author llhuang10
 * @date 2021/10/13 20:18
 */
@Slf4j
@Api(tags = "演员管理模块")
@Validated
@RestController
@RequestMapping("/api/actor")
public class MovieActorController {

    @Resource
    private MovieActorService movieActorService;

    @Resource
    private MovieDetailService movieDetailService;

    /**
     * 添加演员
     *
     * @param movieActor 演员信息
     * @return R
     */
    @PostMapping
    @ApiOperation("添加演员")
    public R insertActor(@Valid @RequestBody MovieActor movieActor) {
        boolean res = movieActorService.insertActor(movieActor);
        return res ? R.success() : R.fail("添加演员失败!, 请联系管理员");
    }


    /**
     * 修改演员信息
     *
     * @param movieActor 演员信息
     * @return R
     */
    @PutMapping
    @ApiOperation("修饰演员信息")
    public R updateActor(@Valid @RequestBody MovieActor movieActor) {
        boolean res = movieActorService.updateActor(movieActor);
        return res ? R.success() : R.fail("修改演员信息!, 请联系管理员");
    }

    /**
     * 根据id获取演员详细信息
     *
     * @param id 演员Id
     * @return 演员详细信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取演员详细信息")
    public R<MovieActor> getById(@PathVariable("id") String id) {
        MovieActor movieActor = movieActorService.getById(id);
        return movieActor != null ? R.success(movieActor) : R.fail("查询失败");
    }

    /**
     * 返回一个电影的所有演员
     *
     * @param movieId 电影id
     * @return 演员列表
     */
    @GetMapping("/all")
    @ApiOperation("返回一个电影的所有演员")
    public R<List<MovieActor>> getAllActorByMovieId(@RequestParam("movieId") int movieId) {
        List<MovieActor> movieActorList = movieDetailService.getAllActorByMovieId(movieId);
        return CollectionUtils.isNotEmpty(movieActorList) ? R.success(movieActorList) : R.fail("查询失败");
    }
}
